package com.yc.mvplib.utlis;

import android.content.Context;
import android.content.SharedPreferences;

import com.yc.mvplib.Basis.BasisApliction;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Function: SharedPreferences工具类
 * Desc:
 */
public class SPUtil {
    private static SPUtil appConfig=null;
    public static final String SP_NAME="appConfig";
    private SharedPreferences preferences;

    private SPUtil() {
        preferences = BasisApliction.getInstanceContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }
    public static SPUtil getInstance() {
        if (appConfig == null)
            appConfig = new SPUtil();
        return appConfig;
    }

    public void putInt(String key, int value) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.putInt(key, value);
        SharedPreferencesCompat.apply(editor);
    }

    public int getInt(String key, int defValue) {
        return preferences.getInt(key, defValue);
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.putString(key, value);
        SharedPreferencesCompat.apply(editor);
    }

    public String getString(String key, String defValue) {
        return preferences.getString(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.putBoolean(key, value);
        SharedPreferencesCompat.apply(editor);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return preferences.getBoolean(key, defValue);
    }

    public void putLong(String key, long value) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.putLong(key, value);
        SharedPreferencesCompat.apply(editor);
    }

    public long getLong(String key, long defValue) {
        return preferences.getLong(key, defValue);
    }

    public void putFloat(String key, float value) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.putFloat(key, value);
        SharedPreferencesCompat.apply(editor);
    }

    public float getFloat(String key, float defValue) {
        return preferences.getFloat(key, defValue);
    }


    /**
     * 移除某个key值已经对应的值
     *
     * @param key
     */
    public  void remove(String key) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 清除所有数据
     *
     * @param context
     */
    public  void clear(Context context) {
        SharedPreferences.Editor editor= preferences.edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 查询某个key是否已经存在
     *
     * @param context
     * @param key
     * @return
     */
    public  boolean contains(Context context, String key) {
        return preferences.contains(key);
    }

    /**
     * 返回所有的键值对
     *
     * @param context
     * @return
     */
    public  Map<String, ?> getAll(Context context) {
        return preferences.getAll();
    }

    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     *
     * @author zhy
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         *
         * @return
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行，否则使用commit
         *
         * @param editor
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            editor.commit();
        }
    }

}
